package com.sskj.mine.ui.activity;


import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.rxbus.ScanBean;
import com.sskj.mine.presenter.WalletAddressAddActivityPresenter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.MINE_WALLET_ADDRESS_ADD)//钱包地址添加
public class WalletAddressAddActivity extends BaseActivity<WalletAddressAddActivityPresenter> {
    @BindView(R2.id.etAddress)
    EditText etAddress;
    @BindView(R2.id.ivScan)
    ImageView ivScan;
    @BindView(R2.id.etRemark)
    EditText etRemark;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @Autowired
    String code;
    @Autowired
    String type;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_wallet_address_add;
    }

    @Override
    public WalletAddressAddActivityPresenter getPresenter() {
        return new WalletAddressAddActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mineWalletAddressAddActivity0));
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        ClickUtil.click(ivScan, () -> {
            new RxPermissions(this)
                    .request(Manifest.permission.CAMERA)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            startActivityForResult(new Intent(mActivity, QuickMarkActivity.class), 1104);
                        } else {
                            PermissionTipUtil.tip(this, App.INSTANCE.getString(R.string.mineWalletAddressAddActivity1));
                        }
                    });
        });
        ClickUtil.click(btSubmit, () -> {
            String address = etAddress.getText().toString().trim();
            if (TextUtils.isEmpty(address)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineWalletAddressAddActivity2));
            } else {
                mPresenter.addWalletAddress(type, address, type, etRemark.getText().toString());
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void scanAddress(ScanBean scanBean) {
        etAddress.setText(scanBean.getAddress());
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
